import { Component, OnInit } from '@angular/core';
import { Router } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";

declare var $: any;
@Component({
	selector: 'sa-path-correction',
	templateUrl: './path-correction.component.html',
})
export class PathCorrectionComponent implements OnInit {
	validatorOptions = {
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			deviceId: {
				validators: {
					notEmpty: {
						message: '请输入设备ID'
					}
				}
			},
			fileToUpload: {
				validators: {
					notEmpty: {
						message: '请上传文件'
					}
				}
			}
		}
	}
	deviceId = '';
	fileToUpload: File = null;
	fileStr = '请选择需要上传的文件';
	constructor(private router: Router, private jsonApiService: JsonApiService) {}
	uploadFile() {
		let postParam = new FormData();
		postParam.append('deviceId', this.deviceId);
		postParam.append('file', this.fileToUpload);
		this.jsonApiService.form_http("track/rectifyfile.html", postParam).subscribe(data => {
			this.jsonApiService.message(data.message)
		})
	}
	onSubmit() {
		$('#pathCorrectionFrom').bootstrapValidator('validate'); //开启验证
		let isValid = $('#pathCorrectionFrom').data('bootstrapValidator').isValid(); //验证是否通过
		if(isValid) {
			this.uploadFile();
		}
	}
	fileChange(files: FileList) {
		this.fileToUpload = files.item(0);
		if(this.fileToUpload) {
			this.fileStr = this.fileToUpload.name;
		} else {
			this.fileStr = '请选择需要上传的文件';
		}
		console.log(this.fileToUpload)
	}
	ngOnInit() {}
}